package com.teach.controller.certificate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.commons.Id;
import com.teach.dto.CertificateInfoSearch;
import com.teach.dto.Search;
import com.teach.entity.CertificateInfo;
import com.teach.entity.CertificateType;
import com.teach.mapper.CertificateInfoMapper;
import com.teach.service.CertificateInfoService;
import com.teach.service.CertificateTypeService;
import com.teach.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * 
 * 创建时间:	2019年12月18日下午4:04:26
 * 当前版本: V1.0
 * 版权说明: Copyright © 1998 - 2019 cyc.top. All Rights Reserved
 *<pre>
 * @author cyc
 */
@Controller
@RequestMapping("/certificate")
@Slf4j
public class CertificateinfoController {
	
	@Autowired
	private CertificateInfoService InfoService;
	
	@Autowired
	private CertificateInfoMapper mapper;
	
	@Autowired
	private Id id;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	CertificateTypeService TypeService;
	
	CertificateInfo certificateInfo = new CertificateInfo();
	/**
	 * 添加
	 * @param certificateInfo
	 */
	
	@RequestMapping("/add.do")
//	@ResponseBody
	public String doCertificateInfoSelectAll(HttpServletRequest req,CertificateInfo certificateInfo) {
//		InfoService.add(certificateInfo);
		certificateInfo.setInfoId(id.nextId());
		certificateInfo.setInfoCertificateId(id.nextId());
		certificateInfo.setInfoCertificateTypeId(id.nextId());
		certificateInfo.setInfoName(req.getParameter("infoName"));
		certificateInfo.setInfoAddress(req.getParameter("infoSex"));
		certificateInfo.setInfoCard(req.getParameter("infoCard"));
		certificateInfo.setInfoPhone(req.getParameter("infoPhone"));	
		certificateInfo.setInfoAddress(req.getParameter("infoAddress"));
		certificateInfo.setInfoImg(req.getParameter("infoImg"));
		certificateInfo.setInfoGrade(Integer.valueOf(req.getParameter("infoGrade")));
		certificateInfo.setInfoPass(req.getParameter("infoPasss"));
		certificateInfo.setInfoRemark(req.getParameter("infoRemark"));
		certificateInfo.setInfoExamUsername(req.getParameter("infoExamUsername"));
		certificateInfo.setInfoRzsUsername(req.getParameter("infoRzsUsername"));
		certificateInfo.setInfoStatus(Integer.valueOf(req.getParameter("infoStatus")));
		certificateInfo.setInfoMoney(Double.valueOf(req.getParameter("infoMoney")));
		certificateInfo.setInfoZfbNumber(id.nextId());
		log.debug("实体：{}",certificateInfo);
		InfoService.add(certificateInfo);
		log.debug("进入：{}",certificateInfo);
		return "/certificate/add";
		
	}
	
	/**
	 * 修改
	 * @param certificateInfo
	 */
	
	@RequestMapping("/update.do")
	public ModelAndView doCertificateInfoGet(HttpServletRequest req,Model model) {
		log.debug("进入");
//		InfoService.update(certificateInfo);
		CertificateInfo certificateInfo = new CertificateInfo();
		certificateInfo = InfoService.getInfoId(Long.valueOf(req.getParameter("infoId")));
		log.debug("打印实体:{}",certificateInfo);
		ModelAndView modelAndView = new ModelAndView();
		model.addAttribute("certificate",certificateInfo);
//		modelAndView.setViewName("certificate/update");
		modelAndView.setViewName("manage/certificate/certificate-update");
		return modelAndView;
		
		
	}
	
	@RequestMapping("/updateEnd.do")
	public String doupdate(HttpServletRequest req) {
		try {
			certificateInfo = InfoService.getInfoId(Long.valueOf(req.getParameter("infoId")));
			log.debug("打印实体2:{}",certificateInfo);
//      修改部分数据
		certificateInfo.setInfoGrade(Integer.valueOf(req.getParameter("infoGrade")));
		certificateInfo.setInfoPass(req.getParameter("infoPass"));
		certificateInfo.setInfoRemark(req.getParameter("infoRemark"));
		certificateInfo.setInfoStatus(req.getParameter("infoStatus")=="已认证"? 1 : 0);
		certificateInfo.setInfoRzsUsername(req.getParameter("infoRzsUsername"));
		log.debug("进入并打印实体：{}",certificateInfo);
		
		//调用service中的update方法
		InfoService.update(certificateInfo);
		log.debug("进入并打印实体ddd：{}",certificateInfo);
		
		log.debug("修改成功");
		}catch (Exception e) {
			return "certificate/failed";		
		}
		log.debug("OK");
		//成功后并跳转至查询所有界面
		return "manage/certificate/certificate-update-success";
	}
	
	
	/**
	 * 删除（Info_Id）
	 * @param certificateInfo
	 * @param infoId
	 */
	
	@RequestMapping("/delete.do")
//	@ResponseBody
	public String doCertificateInfoGet1(HttpServletRequest req) {
		log.debug("进入");
//		certificateInfo.setInfoId(certificateInfo.getInfoId());
//		int delete = mapper.deleteByPrimaryKey(infoId);
//		String delete = InfoService.delete(infoId);
		//测试
//		String delete = InfoService.delete(infoId);
		Long infoid = Long.valueOf(req.getParameter("infoId"));
		log.debug("{}", infoid);
		String delete = InfoService.delete(infoid);
		log.debug("删除成功");
		return "manage/certificate/certificate-delete-success";
	}
	
	/**
	 * 查询所有certificateInfo
	 * @param certificateInfo
	 * @param infoId
	 * @param model
	 * @return
	 */
	
	
	  @RequestMapping("/certificateInfoListAll1.do") 
	  public String doGetListAll(Model model,HttpServletRequest request) {
		  Search s = new Search();
		  int pg1=1;
		  try {
		          	pg1=Integer.valueOf(request.getParameter("pg"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		  s.setCurrent(pg1);
		  s.setRowMax(5);
		  List<CertificateInfo> certificateInfoList = InfoService.listAll(); model.addAttribute("certificateInfoList",certificateInfoList);
		  log.debug("打印: {}",certificateInfoList); 
		  PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax()).doSelectPageInfo(() -> InfoService.listAll());
		  model.addAttribute("list", pageInfo);
		  log.debug("打印：{}"+pageInfo);
		  return "manage/certificate/certificate-info-selectAll2";
	  
	  }
	 
	
	@RequestMapping("/certificaterzs-res-ajax.do")
	@ResponseBody
	public Object doAjaxRes(CertificateInfoSearch search) {
		log.debug("{}", search);
		return InfoService.page(search);
	}

	@RequestMapping("/certificateInfoListAll.do")
	@ResponseBody
	public List<CertificateType> doListRes() {
		log.debug("{}", InfoService.listAll());
		return TypeService.listAll();
	}
		
	/**
	 * 根据信息编号(InfoId)查询
	 * @param certificateInfo
	 * @param infoId
	 * @param model
	 * @return
	 */
	
	@GetMapping("/certificateInfoinfoId.do")
//	@ResponseBody
	public List<CertificateInfo> doGetInfoId(CertificateInfo certificateInfo,Long infoId,Model model) {
		List<CertificateInfo> certificateInfoId =  (List<CertificateInfo>) InfoService.getInfoId(infoId);
		model.addAttribute("certificateInfoId", certificateInfoId);
		System.out.println(certificateInfo);
		return certificateInfoId;
		
	}
	
	
	/**
	 * 根据证书编号(infoCertificateId)查询
	 * @param certificateInfo
	 * @param infoCertificateId
	 * @param model
	 * @return
	 */
	
	
	//成绩未通过已认证
	@RequestMapping("/selectInfoPass.do")
	public String infoPass(Model model,CertificateInfoSearch infopass) {
//		model.addAttribute("list",InfoService.getInfoPass(infopass.getInfoPass()));
//		测试方法
		Search s = new Search();
		 PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax()).doSelectPageInfo(() -> InfoService.listAll());
		  model.addAttribute("list", pageInfo);
		model.addAttribute("certificateInfoList",InfoService.getInfoPass("已通过",1));
		return "manage/certificate/certificate-info-selectAll2";
	}
	
	//查询已通过的考生信息
	@RequestMapping("/selectInfoStatus.do")
	public String infoStatus(Model model,CertificateInfoSearch infoStatus,HttpServletRequest req) {
//		model.addAttribute("list",InfoService.getinfoStatus(infoStatus.getInfoStatus()));
//		测试方法
		Search s = new Search();
		 PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax()).doSelectPageInfo(() -> InfoService.listAll());
		  model.addAttribute("list", pageInfo);
		model.addAttribute("certificateInfoList",InfoService.getinfoStatus("已通过",0));
		return "manage/certificate/certificate-info-selectAll2";
	}
	
	
	//查询未认证
	@RequestMapping("/selectInfoStatus2.do")
	public String infoStatus2(Model model,CertificateInfoSearch infoStatus,HttpServletRequest req) {
		Search s = new Search();
		 PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax()).doSelectPageInfo(() -> InfoService.listAll());
		  model.addAttribute("list", pageInfo);
		model.addAttribute("certificateInfoList",InfoService.getinfoStatus(1));
		return "manage/certificate/certificate-info-selectAll2";
	}
	
	//查询已认证
		@RequestMapping("/selectInfoStatus3.do")
		public String infoStatus3(Model model,CertificateInfoSearch infoStatus,HttpServletRequest req) {
//			model.addAttribute("list",InfoService.getinfoStatus(infoStatus.getInfoStatus()));
//			测试方法
			Search s = new Search();
			 PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax()).doSelectPageInfo(() -> InfoService.listAll());
			  model.addAttribute("list", pageInfo);
			model.addAttribute("certificateInfoList",InfoService.getinfoStatus3(0));
			return "manage/certificate/certificate-info-selectAll2";
		}
		
		
}
